﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileStreamsAndCrypto
{
    class SearchTextFiles
    {
        static public void ExamineDir(string path, string searched, StreamWriter sw)
        {
            ExamineFiles(path, searched, sw);
            foreach (string strDirName
                 in Directory.GetDirectories(path, "*"))
            {
                //DirectoryInfo dirInfo = new DirectoryInfo(strDirName);
                // imeto na direktoriqta bez putq
                //Console.WriteLine(dirInfo.Name);
                // imeto na direktoriqta
                //Console.WriteLine(strDirName);
                ExamineFiles(strDirName, searched, sw);
                ExamineDir(strDirName, searched, sw);
            }
        }

        static public void ExamineFiles(string path, string searched, StreamWriter sw)
        {
            foreach (string strFileName
                 in Directory.GetFiles(path, "*.txt"))
            {
                StreamReader sr = new StreamReader(strFileName);
                using (sr)
                {
                    string contents = sr.ReadToEnd();
                    if (contents.Contains(searched))
                    {
                        sw.WriteLine(strFileName);
                        //sw.Flush();
                    }
                }
            }
        }

        //static void Main()
        //{
        //    StreamWriter sw = new StreamWriter("result.xt1");
        //    using (sw)
        //    {
        //        ExamineDir(@"C:\Users\Academy\Desktop", "abc", sw);
        //    }
        //}
    }
}
